package com.yanfan.energy.config;

import com.alibaba.fastjson.JSON;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.energy.domain.DeviceThings;
import com.yanfan.energy.domain.OverallEnergys;
import com.yanfan.energy.domain.statist.DateStatistics;
import com.yanfan.energy.domain.thingsModel.ThingsModelSimpleItem2;
import com.yanfan.energy.mapper.DeviceThingsMapper;
import com.yanfan.energy.mapper.OverallEnergyMapper;
import com.yanfan.mq.model.ReportDataBo;
import com.yanfan.mq.service.IEnergyMqttService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EnergyMqttServiceImpl implements IEnergyMqttService {

    @Resource
    private DeviceThingsMapper deviceThingsMapper;
    @Resource
    private OverallEnergyMapper overallEnergyMapper;

    @Override
    public void pushMessage(ReportDataBo reportDataBo) {
        addOverallEnergy(reportDataBo);
    }

    public void addOverallEnergy(ReportDataBo bo) {
        // 根据设备编号查询设备参数绑定能源表的数据，根据绑定的数据同步到能源表中
        List<DeviceThings> deviceThings = deviceThingsMapper.findDeviceThingsById(bo.getSerialNumber());

        if (!deviceThings.isEmpty()) {
            try {
                String message = JSON.toJSONString(bo.getDataList());
                List<ThingsModelSimpleItem2> thingsModelSimpleItems = com.alibaba.fastjson.JSON.parseArray(message, ThingsModelSimpleItem2.class);
                thingsModelSimpleItems.stream().forEach(t -> {
                    List<DeviceThings> deviceThingsList = deviceThings
                            .stream()
                            .filter(obj -> obj.getThingsProperty().equals(t.getId()))
                            .collect(Collectors.toList());
                    deviceThingsList.stream().forEach(de -> {
                        OverallEnergys overallEnergys = new OverallEnergys();
                        overallEnergys.setType(de.getEnergyType().intValue());
                        if (de.getEnergyType() == 7 || de.getEnergyType() == 8 || de.getEnergyType() == 9 || de.getEnergyType() == 10) {
                            overallEnergys.setTotalConsumption(t.getValue());
                            overallEnergys.setClassCode(2);
                        } else if (de.getEnergyType() == 1 || de.getEnergyType() == 2 || de.getEnergyType() == 3) {
                            overallEnergys.setTotalDosage(t.getValue());
                            overallEnergys.setClassCode(1);
                        } else {
                            overallEnergys.setTotalDosage(t.getValue());
                            overallEnergys.setClassCode(3);
                        }
                        List<DateStatistics> dateStatistics = overallEnergyMapper.findEnergySetting();
                        List<DateStatistics> dateStatisticsList = dateStatistics
                                .stream()
                                .filter(da -> da.getCount().equals(de.getEnergyType()))
                                .collect(Collectors.toList());
                        if (!dateStatisticsList.isEmpty()) {
                            overallEnergys.setPrice(dateStatisticsList.get(0).getNumber());
                        }
                        overallEnergys.setWorkShopId(de.getWorkshopId());
                        overallEnergys.setDeviceId(de.getDeviceId().longValue());
                        overallEnergys.setCreatedDate(DateUtils.getNowDate());
                        overallEnergys.setUseDate(DateUtils.getNowDate());
                        System.out.println("E21添加成功！！！");
                        // 新增到能源表数据
                        overallEnergyMapper.insert(overallEnergys);
                        overallEnergyMapper.insertDay(overallEnergys);
                    });
                });
            } catch (Exception e) {
                System.out.println("接收属性数据，解析数据时异常 message={}" + e.getMessage());
            }
        }
    }
}
